import { getToken } from '@/utils/auth'

// const baseURL = 'https://ht.rhzsz.com/api2/jialife/';
const baseURL2 = 'https://jiash.huntkey.com/api/jialife/';
const baseURL = 'https://jiash.huntkey.com:8443/djk/jialife/';
const service = (options) => {
	return new Promise((resolve, reject) =>{
		var headers = {'Content-Type':'application/json'};
		if(options.headers){
			var headers=options.headers;
		}
		let token = uni.getStorageSync('uni_session')//uni.getStorageSync('uni_session')//getToken()
		if (token) {
			headers['Authorization'] = token//uni.getStorageSync('uni_session')
		}
		console.log(options, 'options');
		uni.request({
			// url:  (options.urlType === 2 ? baseURL2 : baseURL) + options.url,
			url:  baseURL2 + options.url,
			method: options.method,
			data: options.data,
			header: headers,
			success: (response) => {
				const data = response.data
				let code = data.code
				if(code != 200){
					// localStorage.setItem("uni_session","")
					if(data.code == 401 || data.code == '401'){
						uni.setStorageSync("uni_session","")
						uni.setStorageSync("userInfo","")
						// uni.showModal({
						// 	title: data.message,
						// 	showCancel: false,
						// 	complete: () => {
								// uni.redirectTo({
								// 	url: '/pages/login/login'
								// })
						// 	}
						// })
						// uni.showToast({
						// 	icon:'none',
						// 	title:data.message
						// })
						if (options.url != 'signIn/listIsSign') {
							setTimeout(()=>{
								uni.redirectTo({
									url: '/pages/login/login'
								})
							},500)
						}
					}else{
						uni.showModal({
							title: data.message,
							showCancel:false
						})
						reject(data)
					}
				}else{
					resolve(data.data)
				}
			},
			fail: (res) => {
				uni.showModal({
					title:"操作失败"
				})
				reject(res)
			}
		})
	})
}

export default service
